package com.jumaojiang.service;

import com.jumaojiang.mapper.JeeUserMapper;
import com.jumaojiang.mapper.ResourceGetTimeMapper;
import com.jumaojiang.pojo.JeeUser;
import com.jumaojiang.pojo.JeeUserExample;
import com.jumaojiang.pojo.ResourceGetTime;
import com.jumaojiang.utils.IpAdrressUtil;
import com.jumaojiang.wxpusher.bean.callback.CallBackData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.sql.Time;
import java.util.Date;
import java.util.List;

/**
 * javaee-rs
 *
 * @author wuhanwei
 * @version 1.0
 * @date 2021/11/8
 */
@Service
public class UserService {

    @Autowired
    private JeeUserMapper jeeUserMapper;

    @Autowired
    private ResourceGetTimeMapper resourceGetTimeMapper;


    // 注册添加一个用户
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Integer addUser(JeeUser user){
        return jeeUserMapper.insertSelective(user);
    }

    // 根据uuid查询用户
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public JeeUser queryByUUID(CallBackData callBackData){
        JeeUser user = new JeeUser();
        user.setUserCreateTime(new Date());
        JeeUserExample userExample = new JeeUserExample();
        // 添加uuid查询条件
        JeeUserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUserWxUuidEqualTo(callBackData.getUid());
        List<JeeUser> users = jeeUserMapper.selectByExample(userExample);
        if(users.size() > 0){
            return users.get(0);
        }
        return null;
    }

    // 用户登陆
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public JeeUser login(HttpServletRequest request, CallBackData callBackData, String registration) throws Exception {
        // 查询用户是否注册过
        JeeUser user = queryByUUID(callBackData);
        if(user == null){
            // 未注册, 查询是否允许注册
            if(registration==null || "".equals(registration.trim())){
                throw new Exception("注册不允许,需要授权码");
            }
            JeeUserExample jeeUserExample = new JeeUserExample();
            JeeUserExample.Criteria criteria = jeeUserExample.createCriteria();
            criteria.andRegistrationEqualTo(registration.trim());
            List<JeeUser> jeeUsers = jeeUserMapper.selectByExample(jeeUserExample);
            if(jeeUsers.size() == 0){
                throw new Exception("注册授权码不正确");
            }
            // 销毁注册授权码
            JeeUser jeeUser = jeeUsers.get(0);
            jeeUser.setRegistration(null);
            jeeUserMapper.updateByPrimaryKey(jeeUser);
            user = new JeeUser();
            // 添加用户WxUuid
            user.setUserWxUuid(callBackData.getUid());
            // 添加ip
            String ipAdrress = IpAdrressUtil.getIpAdrress(request);
            user.setUserAddrIp(ipAdrress);
            // 添加创建时间
            user.setUserCreateTime(new Date());
            // 添加用户headImg
            user.setUserWxHeadImg(callBackData.getUserHeadImg());
            // 添加用户name
            user.setUserWxName(callBackData.getUserName());
            // 添加注册人
            user.setRegistrationUserId(jeeUser.getUserId());
            Integer integer = addUser(user);
            if(integer == 0){
                throw new Exception("服务器打了个盹, 再试一次看看");
            }
            // 添加用户可查询次数
            ResourceGetTime resourceGetTime = new ResourceGetTime();
            resourceGetTime.setUserId(user.getUserId());
            resourceGetTime.setEveryDayTime(10);
            resourceGetTime.setTodayTime(10);
            int i = resourceGetTimeMapper.insertSelective(resourceGetTime);
            if(i == 0){
                throw new Exception("服务器打了个盹, 再试一次看看");
            }
        }
        user.setUserLastLoginTime(new Date());
        int i = jeeUserMapper.updateByPrimaryKeySelective(user);
        if(i == 0){
            throw new Exception("服务器打了个盹, 再试一次看看");
        }
        return user;
    }
}
